﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProfileManager.Controls
{
    public partial class ServiceAccountItemList : UserControl
    {
        public Control CurrentItem { get; set; }

        public int ItemCount { get; set; }

        public int ActualItemCount { get; set; }

        private object itemCreator;

        public event EventHandler ClickItemHandler = null;
        private void ItemClick(object sender, EventArgs e)
        {
            CurrentItem = (Control)sender;
            if (ClickItemHandler != null)
            {
                ClickItemHandler(sender, e);
            }
        }

        public ServiceAccountItemList()
        {
            InitializeComponent();
        }

        public void Init<T, TCreator>() where TCreator : new()
        {
            this.SuspendLayout();

            itemCreator = new TCreator();
            for (int i = 0; i < ItemCount; i++)
            {
                Control item = ((ServiceAccountItem<T>)itemCreator).Create(i) as Control;
                if (item != null)
                {
                    this.Controls.Add(item);
                }
            }
            
            this.ResumeLayout();
        }

        public void AddRange<T>(IEnumerable<T> newItem) where T : Control
        {
            foreach (T item in newItem)
            {
                AddItem(item, null);
            }
        }

        public void AddItem<T>(T newItem, EventHandler eventHandler) where T : Control
        {
            int index = ActualItemCount;

            T item = (T)this.Controls[index];
            item.Click += ItemClick;

            ((ServiceAccountItem<T>)itemCreator).Add(item, newItem, eventHandler);

            item.Size = new Size(386, 24);
            item.Location = new Point(20, 20);

            ActualItemCount++;
        }
    }
}
